﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class AutoFilterInfoRecord : WorksheetLevelRecord
    {
        private ushort _cEntries;

        internal static IEnumerable<BiffRecord> GetAutoFilterRecords (AutoFilter autoFilter)
        {
            yield return new FilterModeRecord();
            yield return new AutoFilterInfoRecord(autoFilter);
            foreach (var column in autoFilter.FilterColumns)
                foreach (var record in AutoFilterRecord.GetAutoFilterRecord(column))
                    yield return record;
        }
        private AutoFilterInfoRecord(AutoFilter autoFilter)
        {
            this._cEntries = (ushort) autoFilter.FilterColumns.Count;
        }

        public AutoFilterInfoRecord ()
        {
        }

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            this._cEntries = context.ReadUInt16();
        }

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.AutoFilterInfo; }
        }

        internal override void Apply(BiffReaderWorksheetContext context)
        {
            context.ParentContext.GetActions(ActionType.ResolveName).Add(delegate
                                                                             {
                                                                                 if (!context.ParentContext.ContainsAutoFilter(context.Worksheet))
                                                                                     return;
                                                                                 var range = context.ParentContext.GetAutofilter(context.Worksheet);
                                                                                 Contract.Assert(range != null);
                                                                                 AutoFilter autoFilter = new AutoFilter(range);
                                                                                 autoFilter.FilterColumns.Clear();
                                                                                 foreach (var item in GetColumns(autoFilter))
                                                                                    autoFilter.FilterColumns.Add(item);
                                                                                 context.Worksheet.AutoFilter = autoFilter;
                                                                                 autoFilter.Apply();

                                                                             });
            
        }

        private IEnumerable<FilterColumn> GetColumns(AutoFilter autoFilter)
        {
            BiffRecord next = this.Next;
            while (next is AutoFilterRecord)
            {
                AutoFilterRecord r = (AutoFilterRecord) next;
                yield return r.GetColumn(autoFilter, autoFilter.Range);
                next = next.Next;
                while (next is AutoFilter12Record || next is ContinueFrt12Record)
                    next = next.Next;
            }
        }


        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            context.Write(_cEntries);
        }
    }
}